import { createRouter, createWebHistory } from 'vue-router'
import { isLoggedIn } from '@/utils/authUtils'
import FirstPage from '@/views/FirstPage.vue'
import LoginPage from '@/views/auth/LoginPage.vue'
import EmailResetPass from '@/views/auth/EmailResetPassword.vue'
import Mine from "@/views/Mine.vue"
import fabu from "@/views/personal/fabu.vue"
import maichu from "@/views/personal/maichu.vue"
import maidao from "@/views/personal/maidao.vue"
import shoucang from "@/views/personal/shoucang.vue"
import tofabu from "@/views/product/tofabu.vue"
import message from "@/views/message.vue"
import Register from "@/views/auth/Register.vue"
import Address from "@/views/personal/Address.vue"
import Settings from "@/views/personal/Settings.vue"
import PhoneResetPassword from "@/views/auth/PhoneResetPassword.vue"
import feedback from "@/views/review/feedback.vue"
import reviewOrder from "@/views/review/reviewOrder.vue"
import password from "@/views/auth/password.vue"
import edit from "@/views/product/edit.vue"
import orders from "@/views/order/orders.vue"
import payment from "@/views/order/payment.vue"

// 路由配置
const routes = [
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/product/SearchPage.vue')
  },
  {
    path: '/product/:product_code',
    name: 'ProductDetail',
    component: () => import('@/views/product/ProductDetail.vue')
  },
  {
    path: '/',
    name: 'Home',
    component: FirstPage
  },
  {
    path: '/login',
    name: 'Login',
    component: LoginPage
  },
  {
    path: '/email-reset-password',
    name: 'EmailResetPass',
    component: EmailResetPass
  },
  {
    path: '/phone-reset-password',
    name: 'PhoneResetPassword',
    component: PhoneResetPassword
  },
  {
    path: '/password',
    name: 'Password',
    component: password
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/tofabu',
    component: tofabu
  },
   {
    path: '/edit',
    component: edit
  },
  {
    path: '/orders',
    component: orders
  },
  {
    path: '/payment',
    component: payment
  },
  {
  path: '/paySuccess',
  name: 'PaySuccess',
  component: () => import('@/views/order/paySuccess.vue')
  },
  {
  path: '/orders/:order_code',
  name: 'ordersDetail',
  component: () => import('@/views/order/ordersDetail.vue')
  },
  {
    path: '/reviewOrder',
    component: reviewOrder
  },
  {
    path: '/orders/:order_code/review',
  name: 'reviewDetail',
  component: () => import('@/views/review/reviewDetail.vue')
  },
  {
    path: '/message',
    component: message
  },
  {
    path: '/Mine',
    name: 'Mine',
    component: Mine,
    meta: { requiresAuth: true }, // 标记需要登录
    redirect: '/Mine/fabu',
    children: [
      { path: 'fabu', component: fabu },
      { path: 'maichu', component: maichu },
      { path: 'maidao', component: maidao },
      { path: 'shoucang', component: shoucang },
      { path: 'Address', component: Address },
      { path: 'Settings', component: Settings },
      { path: 'feedback', component: feedback },
      //{ path: 'review', component: review },
    ]
  },
];

// 检查 Token 是否过期
const checkTokenExpiry = () => {
  const tokenExpire = localStorage.getItem('token_expire');
  if (tokenExpire) {
    const currentTime = Date.now();
    return currentTime < parseInt(tokenExpire); // 比较当前时间和 token 的过期时间
  }
  return false;
};

// 路由配置
const router = createRouter({
  history: createWebHistory(),
  routes
});

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!isLoggedIn.value) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
});

export default router;